﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace myCollection
{
    public partial class DictionaryAdd : Form
    {
        private int id = 0;
        private Dictionary.TypeD type0;

        public DictionaryAdd(Dictionary.TypeD typed, int id0 = 0)
        {
            InitializeComponent();
            this.id = id0;
            this.type0 = typed;
            if (this.id != 0)
            {
                this.enabledForm(false);
                SQLite sqlite = new SQLite();
                sqlite.Connect();
                DataTable dt = new DataTable();
                dt = sqlite.GetDictionary(this.type0, this.id);
                sqlite.Disconnect();
                tbName.Text = dt.Rows[0]["name"].ToString();
            }
        }

        private void enabledForm(bool b)
        {
            tbName.ReadOnly = !b;
            btEdit.Enabled = !b;
            btSave.Enabled = b;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Length == 0)
            {
                MessageBox.Show("Name is empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            SQLite sqlite = new SQLite();
            sqlite.Connect();
            if (this.id == 0)
            {
                if (sqlite.ExistDictionary(this.type0, tbName.Text))
                {
                    MessageBox.Show("Name: " + tbName.Text + " is exist!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (sqlite.AddDictionary(this.type0, tbName.Text))
                {
                    MessageBox.Show("Add successfull!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Add fail!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                if (sqlite.ExistDictionaryUpdate(this.type0, tbName.Text))
                {
                    MessageBox.Show("Name: " + tbName.Text + " is exist!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (sqlite.UpdateDictionary(this.type0, tbName.Text, this.id))
                {
                    MessageBox.Show("Update successfull!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Update fail!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            sqlite.Disconnect();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            this.enabledForm(true);
        }
    }
}
